package com.qinggeng.project.system.service.impl;

import cn.hutool.core.date.DateUtil;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.project.system.domain.UserActivityReaEnvelopes;
import com.qinggeng.project.system.mapper.UserAccountRecordMapper;
import com.qinggeng.project.system.mapper.UserActivityReaEnvelopesMapper;
import com.qinggeng.project.system.service.UserActivityRadEnvelopesService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class UserActivityRadEnvelopesServiceImpl implements UserActivityRadEnvelopesService {

    @Resource
    private UserActivityReaEnvelopesMapper userActivityReaEnvelopesMapper;

    @Override
    public boolean checkUserActivityRedEnvelopes(Long userId, String activityKey,Date date) {
        UserActivityReaEnvelopes envelopes= getUserActivityRedEnvelopes( userId,activityKey,date);
        if(null !=envelopes){
            throw  new CustomException("今天已经参与过活动了");
        }
        return true;
    }

    @Override
    public UserActivityReaEnvelopes getUserActivityRedEnvelopes(Long userId, String activityKey, Date date) {
        UserActivityReaEnvelopes lops=new UserActivityReaEnvelopes();
        lops.setUserId(userId);
        lops.setActivityDate(DateUtil.formatDate(date));
        lops.setActivityKey(activityKey);
        UserActivityReaEnvelopes envelopes= getUserActivityReaEnvelopes(lops);
        return envelopes;
    }

    @Override
    public boolean insertUserActivityRedEnvelopes(UserActivityReaEnvelopes userActivityReaEnvelopes) {
                if (null == userActivityReaEnvelopes){
                    throw  new CustomException("userActivityReaEnvelopes is null");
                }
                Long userId= userActivityReaEnvelopes.getUserId();
                if (null == userId){
                    throw  new CustomException("userId is null");
                }
                 String key= userActivityReaEnvelopes.getActivityKey();
                if (StringUtils.isEmpty(key)){
                    throw  new CustomException("key is null");
                }
                userActivityReaEnvelopes.setActivityDate(DateUtils.getDate());
                Date date=new Date();
                userActivityReaEnvelopes.setCreateTime(date);
                userActivityReaEnvelopes.setUpdateTime(date);
        return userActivityReaEnvelopesMapper.insertSelective(userActivityReaEnvelopes)>=1;
    }

    private  UserActivityReaEnvelopes getUserActivityReaEnvelopes(UserActivityReaEnvelopes UserActivityReaEnvelopes){

        Example example =new Example(UserActivityReaEnvelopes.class);
        example.createCriteria()
                .andEqualTo("userId",UserActivityReaEnvelopes.getUserId())
                .andEqualTo("activityDate",UserActivityReaEnvelopes.getActivityDate())
                .andEqualTo("activityKey",UserActivityReaEnvelopes.getActivityKey());
        UserActivityReaEnvelopes userac=userActivityReaEnvelopesMapper.selectOneByExample(example);
        return  userac;
    }


}
